{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "e94c85cd-f364-4916-acee-69ff05fc86b3",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'C:\\\\Users\\\\HP\\\\Desktop\\\\饼状图.html'"
      ]
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import pandas as pd    \n",
    "from pyecharts import options as opts    \n",
    "from pyecharts.charts import Pie,Timeline     \n",
    "from pyecharts.globals import ThemeType\n",
    "\n",
    "df_world = pd.read_excel(\"C:\\\\Users\\\\HP\\\\Desktop\\\\GDP (C).xlsx\", sheet_name=\"全球GDP\", index_col=0) \n",
    "countries = df_world.index.tolist()\n",
    "yearlist = df_world.columns.tolist()\n",
    "year_gdp = []\n",
    "for year in df_world.columns:     \n",
    "    df_year = df_world[[year]] \n",
    "    df_year.columns = ['GDP']  \n",
    "    gdps = df_year['GDP'].tolist() \n",
    "    year_gdp.append(gdps)\n",
    "data = dict(zip(yearlist,year_gdp))\n",
    "\n",
    "tl = Timeline(init_opts=opts.InitOpts(theme=ThemeType.LIGHT))\n",
    "for i in yearlist:\n",
    "    c = (\n",
    "    Pie(init_opts=opts.InitOpts(theme=ThemeType.ROMANTIC))    \n",
    "    .add(\"\",[list(z) for z in zip(countries,data[i])])\n",
    "    .set_global_opts(title_opts=opts.TitleOpts(title=f\"{i}年部分国家GDP对比\",pos_top=\"top\",pos_left=\"left\"),\n",
    "                    legend_opts=opts.LegendOpts(pos_left=\"right\", orient=\"vertical\"))      \n",
    "    .set_series_opts(label_opts=opts.LabelOpts(formatter='{b}:{d}%')))  \n",
    "    tl.add(c,i)\n",
    "    \n",
    "tl.render(\"饼状图.html\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "7d91e8e3-f58a-4428-9028-3d59945487fa",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
